import { app, BrowserWindow, ipcMain } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils' // 导入工具函数
import { registerAllIpc } from './api' //所有的api都在这里注册
import { registrationWindow } from './windows' // 注册窗口
import { createWindow } from './windows/main' // 创建主窗口
import './hooks/eventBus' //引入事件总线
app.whenReady().then(() => {
  registerAllIpc()
  registrationWindow()
  electronApp.setAppUserModelId('com.electron')
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })
  // IPC test
  ipcMain.on('ping', () => console.log('pong'))
  ipcMain.handle('print', (_, args: string) => {
    console.log('print', args)
  })
  // 监听窗口创建
  createWindow()
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
